<!--
 * @Author: spycym
 * @Date: 2022-07-14 11:17:27
 * @LastEditTime: 2022-07-16 10:53:25
 * @LastEditors: spycym
 * @Description: 
 * 不要随便动我的代码哟!
-->
<template>
  <li>
    <label>
      <input
        type="checkbox"
        :checked="todo.done"
        @change="changeDone(todo.id)"
      />
      <span v-show="!todo.isEdit">{{ todo.thing }}</span>
      <input
        type="text"
        :value="todo.thing"
        v-show="todo.isEdit"
        @blur="handleBlur(todo, $event)"
        ref="inputValue"
      />
    </label>
    <button class="btn btn-danger" @click="deleteTodo(todo.id)">删除</button>
    <button
      class="btn btn-edit"
      @click="UpdateTodo(todo)"
      v-show="!todo.isEdit"
    >
      编辑
    </button>
  </li>
</template>

<script>
export default {
  name: "todoItem",
  props: ["todo"],
  methods: {
    deleteTodo(id) {
      if (confirm("是否确认删除该事件？")) this.$bus.$emit("deleteTodoObj", id);
    },
    changeDone(id) {
      this.$bus.$emit("changeTodoDone", id);
    },
    UpdateTodo(todo) {
      if ("isEdit" in todo) {
        todo.isEdit = true;
      } else {
        this.$set(todo, "isEdit", true);
      }
      this.$nextTick(function () {
        this.$refs.inputValue.focus();
      });
    },
    handleBlur(todo, e) {
      todo.isEdit = false;
      if (!e.target.value.trim()) return alert("请输入有效字符！");
      this.$bus.$emit("UpdateTodoObj", todo.id, e.target.value);
    },
  },
};
</script>

<style scoped>
/*item*/
li {
  list-style: none;
  height: 36px;
  line-height: 36px;
  padding: 0 5px;
  border-bottom: 1px solid #ddd;
}

li label {
  float: left;
  cursor: pointer;
}

li label li input {
  vertical-align: middle;
  margin-right: 6px;
  position: relative;
  top: -1px;
}

li button {
  float: right;
  display: none;
  margin-top: 3px;
}

li:before {
  content: initial;
}

li:last-child {
  border-bottom: none;
}
li:hover button {
  display: block;
}
</style>
